unicsv: Avoid a few time_t conversions that would lose sub-second.
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 5 Oct 2014 23:16:08 +0000 (23:16 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 5 Oct 2014 23:16:08 +0000 (23:16 +0000)
Various: Less CSTR() abuse.
garmin_tables. Use QString as arg type.

gpsbabel/csv_util.cc
gpsbabel/garmin_tables.cc
gpsbabel/garmin_tables.h
gpsbabel/gpx.cc
gpsbabel/ozi.cc
gpsbabel/text.cc
gpsbabel/unicsv.cc
gpsbabel/util.cc
gpsbabel/xol.cc

index 8e979fce43cbade55b248ca4fc54434efbc514d8..4bf9d01ceb8629f7b729a70e2ed20535c6cbeaef 100644 (file)
@@ -881,10 +881,9 @@ addhms(const char* s, const char* format)
   int  hour =0;
   int  min  =0;
   int  sec  =0;
-  char* ampm = NULL;
   int ac;
 
-  ampm = (char*) xmalloc(strlen(s) + 1);
+  char* ampm = (char*) xmalloc(strlen(s) + 1);
   ac = sscanf(s, format, &hour, &min, &sec, ampm);
   /* If no time format in arg string, assume AM */
   if (ac < 4) {
index cd48db6cc12dfdc4219d02a982e9fb3ffc2878c8..40412879d87f5689c7cee9cd042ed357b68e8dcb 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "garmin_tables.h"
+#include "src/core/logging.h"
 #include "jeeps/gpsmath.h"
 
 #include <stdio.h>
@@ -965,19 +966,19 @@ gt_get_icao_cc(const QString& country, const QString& shortname)
 }
 
 grid_type
-gt_lookup_grid_type(const char* grid_name, const char* module)
+gt_lookup_grid_type(const char* grid_name, const QString& module)
 {
   grid_mapping_t* g;
 
   for (g = gt_mps_grid_names; (g->shortname); g++) {
-    if ((case_ignore_strcmp(grid_name, g->shortname) == 0) ||
-        (case_ignore_strcmp(grid_name, g->longname) == 0)) {
+    if (QString::compare(grid_name, g->shortname, Qt::CaseInsensitive) == 0 ||
+        QString::compare(grid_name, g->longname,Qt::CaseInsensitive) == 0) {
       return g->grid;
     }
   }
 
-  fatal("%s: Unsupported grid (%s)! See GPSBabel help for supported grids.\n",
-        module, grid_name);
+  Fatal() << module << ": Unsupported grid (" << grid_name << 
+                       ". See GPSBabel help for supported grids.\n";
 
   return grid_unknown; /* (warnings) */
 }
@@ -1001,7 +1002,7 @@ gt_get_mps_datum_name(const int datum_index)
   result = GPS_Math_Get_Datum_Name(datum_index);
 
   for (d = gt_mps_datum_names; (d->jeeps_name); d++)
-    if (case_ignore_strcmp(result, d->jeeps_name) == 0) {
+    if (QString::compare(result, d->jeeps_name, Qt::CaseInsensitive) == 0) {
       return d->mps_name;
     }
 
@@ -1009,14 +1010,14 @@ gt_get_mps_datum_name(const int datum_index)
 }
 
 int
-gt_lookup_datum_index(const char* datum_str, const char* module)
+gt_lookup_datum_index(const char* datum_str, const QString& module)
 {
   datum_mapping_t* d;
   int result;
   const char* name = datum_str;
 
   for (d = gt_mps_datum_names; (d->jeeps_name); d++) {
-    if (case_ignore_strcmp(name, d->mps_name) == 0) {
+    if (QString::compare(name, d->mps_name, Qt::CaseInsensitive) == 0) {
       name = d->jeeps_name;
       break;
     }
@@ -1024,15 +1025,16 @@ gt_lookup_datum_index(const char* datum_str, const char* module)
 
   result = GPS_Lookup_Datum_Index(name);
 
+  // Didn't get a hit?  Try again after modifying the lookup.
   if (result < 0) {
     QString tmp = QString(datum_str) + " mean";
-    result = GPS_Lookup_Datum_Index(CSTR(tmp));
+    result = GPS_Lookup_Datum_Index(tmp);
   }
 
-  is_fatal(result < 0,
-           "%s: Unsupported datum (%s)! See GPSBabel help for supported datums.",
-           module, datum_str);
-
+  if (result < 0) {
+    Fatal() << module << ": Unsupported datum (" << datum_str << 
+                         "). See GPSBabel help for supported datums.";
+  }
   return result;
 }
 
@@ -1047,12 +1049,12 @@ gt_color_value(const unsigned int garmin_index)
 }
 
 uint32_t
-gt_color_value_by_name(const char* name)
+gt_color_value_by_name(const QString& name)
 {
   unsigned int i;
 
   for (i = 0; i < GT_COLORS_CT; i++)
-    if (case_ignore_strcmp(gt_colors[i].name, name) == 0) {
+    if (QString::compare(gt_colors[i].name, name, Qt::CaseInsensitive) == 0) {
       return gt_colors[i].rgb;
     }
 
@@ -1060,12 +1062,12 @@ gt_color_value_by_name(const char* name)
 }
 
 int
-gt_color_index_by_name(const char* name)
+gt_color_index_by_name(const QString& name)
 {
   unsigned int i;
 
   for (i = 0; i < GT_COLORS_CT; i++)
-    if (case_ignore_strcmp(name, gt_colors[i].name) == 0) {
+    if (QString::compare(gt_colors[i].name, name, Qt::CaseInsensitive) == 0) {
       return i;
     }
 
index b3069495cf9f4102689dabeac5a19353e78d6a83..2e2252909eae2f95b1b06bc71f2ca0594fb7bee0 100644 (file)
@@ -91,13 +91,13 @@ unsigned char gt_convert_category(const char* name, int* category);
 
 unsigned char gt_switch_display_mode_value(const unsigned char display_mode, const int protoid, const char device);
 
-grid_type gt_lookup_grid_type(const char* grid_name, const char* module);
+grid_type gt_lookup_grid_type(const char* grid_name, const QString& module);
 const char* gt_get_mps_grid_longname(const grid_type grid, const char* module);
-int gt_lookup_datum_index(const char* datum_str, const char* module);
+int gt_lookup_datum_index(const char* datum_str, const QString& module);
 const char* gt_get_mps_datum_name(const int datum_index);
 uint32_t gt_color_value(const unsigned int garmin_index);
-uint32_t gt_color_value_by_name(const char* name);
-int gt_color_index_by_name(const char* name);
+uint32_t gt_color_value_by_name(const QString& name);
+int gt_color_index_by_name(const QString& name);
 int gt_color_index_by_rgb(const int rgb);
 const char* gt_color_name(const unsigned int garmin_index);
 
index 38e3b15851232277415f1d8dce8bee167f0f3a87..7e9337aa41022dbcd8c5143cca64ee44bf535d67 100644 (file)
@@ -1033,7 +1033,7 @@ gpx_end(const QString& el)
     rte_head->rte_desc = cdatastr;
     break;
   case tt_garmin_rte_display_color:
-    rte_head->line_color.bbggrr = gt_color_value_by_name(CSTR(cdatastr));
+    rte_head->line_color.bbggrr = gt_color_value_by_name(cdatastr);
     break;
   case tt_rte_number:
     rte_head->rte_num = cdatastr.toInt();
@@ -1064,7 +1064,7 @@ gpx_end(const QString& el)
     trk_head->rte_desc = cdatastr;
     break;
   case tt_garmin_trk_display_color:
-    trk_head->line_color.bbggrr = gt_color_value_by_name(CSTR(cdatastr));
+    trk_head->line_color.bbggrr = gt_color_value_by_name(cdatastr);
     break;
   case tt_trk_number:
     trk_head->rte_num = cdatastr.toInt();
index 444d5d3248057754540510eb5aeb049fdd91ac04..015b3865a76a1e39e09a2343735046c02c702580 100644 (file)
@@ -501,7 +501,7 @@ ozi_parse_waypt(int field, const QString& str, Waypoint* wpt_tmp, ozi_fsdata* fs
     break;
   case 1:
     /* waypoint name */
-    wpt_tmp->shortname = csv_stringtrim(str, "");
+    wpt_tmp->shortname = str.trimmed();
     break;
   case 2:
     /* degrees latitude */
@@ -541,7 +541,7 @@ ozi_parse_waypt(int field, const QString& str, Waypoint* wpt_tmp, ozi_fsdata* fs
     break;
   case 10:
     /* Description */
-    wpt_tmp->description = csv_stringtrim(str, "");
+    wpt_tmp->description = str.trimmed();
     break;
   case 11:
     /* pointer direction 0,1,2,3 bottom,top,left,right */
index f908fa5992c07ea14fbe301929a2f2b53e8dae74..bb50d0146f4099ddf9e1159df4dd510a2991a6a5 100644 (file)
@@ -130,9 +130,10 @@ text_disp(const Waypoint* wpt)
   }
 
   if (wpt->description != wpt->shortname) {
-    gbfprintf(file_out, "%s", CSTRc(wpt->description));
+    gbfputs(wpt->description, file_out);
     if (!wpt->gc_data->placer.isEmpty()) {
-      gbfprintf(file_out, " by %s", CSTR(wpt->gc_data->placer));
+      gbfputs(" by ", file_out);
+      gbfputs(wpt->gc_data->placer, file_out);
     }
   }
   if (wpt->gc_data->terr) {
@@ -161,7 +162,9 @@ text_disp(const Waypoint* wpt)
       xfree(hint);
     }
   } else if (!wpt->notes.isEmpty() && (wpt->description.isEmpty() || wpt->notes != wpt->description)) {
-    gbfprintf(file_out, "\n%s\n", CSTRc(wpt->notes));
+    gbfputs("\n", file_out);
+    gbfputs(wpt->notes, file_out);
+    gbfputs("\n", file_out);
   }
 
   fs_gpx = NULL;
@@ -181,12 +184,14 @@ text_disp(const Waypoint* wpt)
 
       logpart = xml_findfirst(curlog, "groundspeak:type");
       if (logpart) {
-        gbfprintf(file_out, "%s by ", CSTR(logpart->cdata));
+        gbfputs(logpart->cdata, file_out);
+        gbfputs(" by ", file_out);
       }
 
       logpart = xml_findfirst(curlog, "groundspeak:finder");
       if (logpart) {
-        gbfprintf(file_out, "%s on ", CSTR(logpart->cdata));
+        gbfputs(logpart->cdata, file_out);
+        gbfputs(" on ", file_out);
       }
 
       logpart = xml_findfirst(curlog, "groundspeak:date");
index fa01961681adabe020e1ea94eaec012644c43e75..c853acf504f5c01fce2b4ce4d62c034199aea981 100644 (file)
@@ -318,7 +318,8 @@ unicsv_parse_gc_id(const char* str)
     } else {
       base = (*str < 'G') ? 16 : 31;
     }
-
+    // FIXME: this is wrong. 0123456789ABCDEFGHJKMNPQRTVWXYZ  ILOSU are omitted.
+    // That nobody has noticed is a good hint nobody cares...
     while ((cx = *str++)) {
       int num;
 
@@ -339,8 +340,6 @@ unicsv_parse_gc_id(const char* str)
   return res;
 }
 
-// static int unicsv_parse_time(const char *str, int *msec, time_t *date);
-
 static time_t
 unicsv_parse_date(const char* str, int* consumed)
 {
@@ -416,7 +415,6 @@ unicsv_parse_time(const char* str, int* msec, time_t* date)
       *date = ldate;
     }
   }
-
   ct = sscanf(str, "%d%1[.://]%d%1[.://]%d%lf", &hour, sep, &min, sep, &sec, &ms);
   is_fatal(ct < 5, MYNAME ": Could not parse time string (%s).\n", str);
   if (ct == 6) {
@@ -1670,52 +1668,38 @@ unicsv_waypt_disp_cb(const Waypoint* wpt)
   }
   if FIELD_USED(fld_date) {
     if (wpt->creation_time.toTime_t() >= SECONDS_PER_DAY) {
-      struct tm tm;
-      char buf[32];
-      time_t time = wpt->GetCreationTime().toTime_t();
-
+      QDateTime dt;
       if (opt_utc) {
-        time += atoi(opt_utc) * SECONDS_PER_HOUR;
-        tm = *gmtime(&time);
+        dt = wpt->GetCreationTime().toUTC();
+        // We might wrap to a different day by overriding the TZ offset.
+        dt = dt.addSecs(atoi(opt_utc) * SECONDS_PER_HOUR);
       } else {
-        const time_t tt = wpt->GetCreationTime().toTime_t();
-        tm = *localtime(&tt);
+        dt = wpt->GetCreationTime();
       }
-      tm.tm_year += 1900;
-      tm.tm_mon += 1;
-      snprintf(buf, sizeof(buf), "%04d/%02d/%02d", tm.tm_year, tm.tm_mon, tm.tm_mday);
-      gbfprintf(fout, "%s%s", unicsv_fieldsep, buf);
+      QString date = dt.toString("yyyy/MM/dd");
+      gbfputs(unicsv_fieldsep, fout);
+      gbfputs(date, fout);
     } else {
       gbfputs(unicsv_fieldsep, fout);
     }
   }
   if FIELD_USED(fld_time) {
     if (wpt->creation_time.isValid()) {
-      struct tm tm;
-      char buf[32], msec[12];
-      time_t time = wpt->GetCreationTime().toTime_t();
-
+      QTime t;
       if (opt_utc) {
-        time += atoi(opt_utc) * SECONDS_PER_HOUR;
-        tm = *gmtime(&time);
+        t = wpt->GetCreationTime().toUTC().time();
+        t = t.addSecs(atoi(opt_utc) * SECONDS_PER_HOUR);
       } else {
-        const time_t tt = wpt->GetCreationTime().toTime_t();
-        tm = *localtime(&tt);
+        t = wpt->GetCreationTime().time();
       }
-      snprintf(buf, sizeof(buf), "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
-
-      int millisecs = wpt->GetCreationTime().time().msec();
-      if (millisecs > 0) {
-        int len = 3;
-
-        while (len && (millisecs % 10 == 0)) {
-          millisecs /= 10;
-          len--;
-        }
-        snprintf(msec, sizeof(msec), ".%0*d", len, millisecs);
-        strcat(buf, msec);
+      QString out;
+      if (t.msec() > 0) {
+        out = t.toString("hh:mm:ss.z");
+      } else {
+        out = t.toString("hh:mm:ss");
       }
-      gbfprintf(fout, "%s%s", unicsv_fieldsep, buf);
+      gbfputs(unicsv_fieldsep, fout);
+      gbfputs(out, fout);
     } else {
       gbfputs(unicsv_fieldsep, fout);
     }
index 076f0bc8792f7869d5b1c1563895fbd1a741f308..eb8ec51156313bbe1b103b1f7f7b87ce858d96aa 100644 (file)
@@ -1,7 +1,7 @@
 /*
     Misc utilities.
 
-    Copyright (C) 2002-2005 Robert Lipe, robertlipe+source@gpsbabel.org
+    Copyright (C) 2002-2014 Robert Lipe, robertlipe+source@gpsbabel.org
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
index 719f0946c005610f10e01fef1bdf0654687f4c70..3829bc2a6e56e7b40fdaae145c4498caf14da440 100644 (file)
@@ -185,8 +185,8 @@ static void
 xol_write_string(const QString& name, const QString& str)
 {
   if (!str.isEmpty()) {
-    QString temp = strenquote(str, '"');
-    gbfprintf(fout, " %s=%s", CSTR(name), CSTR(temp));
+    QString out = name + "=" + strenquote(str, '"');
+    gbfputs(out, fout);
   }
 }